package com.itlxd.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlxd.dto.DishDTO;
import com.itlxd.dto.PageDTO;
import com.itlxd.entity.Dish;
import com.itlxd.exception.ArgsException;
import com.itlxd.exception.SqlOperationException;
import com.itlxd.global.ErrorCode;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.IDishService;
import com.itlxd.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    /**
     * 分页查询菜品列表
     * @param pageDTO
     * @return
     */
    @GetMapping("/page")
    public ResultVO<IPage<DishVO>> queryDishesByPage(PageDTO pageDTO) {
        IPage<DishVO> dishIPage = dishService.listPageDishes(pageDTO);
        return ResultVO.success(dishIPage, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultVO<DishVO> queryDishById(@PathVariable Long id) {
        DishVO dishVO = dishService.getOneById(id);
        if (null == dishVO) {
            return ResultVO.error(SysMessages.QUERY_FAIL);
        }
        return ResultVO.success(dishVO, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 添加菜品和口味
     * @param dishDTO
     * @return
     */
    @PostMapping
    public ResultVO<Integer> addDishAndFlavor(@RequestBody DishDTO dishDTO) {
        if (null == dishDTO) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Integer effectNum = dishService.saveDishAndDishFlavor(dishDTO);
        if (effectNum <= SysConstants.NUM_ZERO) {
            throw new ArgsException(ErrorCode.INSERT_DEFAULT_ERROR, SysMessages.INSERT_FAIL);
        }

        return ResultVO.success(effectNum, SysMessages.INSERT_SUCCESS);
    }

    /**
     * 批量修改菜品状态
     * @param status 状态值
     * @param ids 菜品id集合
     * @return
     */
    @PutMapping("/status/{status}")
    public ResultVO<Integer> dishStatusByStatus(@RequestParam Long[] ids, @PathVariable Integer status) {
        if (null == status || ids.length <= 0 ) {
            throw new ArgsException(SysConstants.ARGS_IS_NULL, SysMessages.ARGS_IS_NULL);
        }

        Integer updateNum = dishService.updateBatchIds(ids, status);
        if (updateNum <= SysConstants.NUM_ZERO) {
            throw new SqlOperationException(ErrorCode.UPDATE_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
        }

        return ResultVO.success(updateNum, SysMessages.UPDATE_SUCCESS);
    }

    /**
     * 更新菜品和口味
     * @param dishDTO
     * @return
     */
    @PutMapping
    public ResultVO<Integer> updateDishWithDishFlavor(@RequestBody DishDTO dishDTO) {
        if (null == dishDTO) {
            throw new ArgsException(SysConstants.ARGS_IS_NULL, SysMessages.ARGS_IS_NULL);
        }

        Integer updateNum = dishService.updateDishWithDishFlavor(dishDTO);
        if (null == updateNum) {
            throw new SqlOperationException(ErrorCode.UPDATE_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
        }

        return ResultVO.success(updateNum, SysMessages.UPDATE_SUCCESS);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResultVO<Integer> batchDeleteDishById(@RequestParam Long[] ids) {
        if (null == ids || ids.length <= SysConstants.NUM_ZERO) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Integer delRows = dishService.deleteBatchIds(ids);
        if (null == delRows || delRows <= SysConstants.NUM_ZERO) {
            throw new SqlOperationException(ErrorCode.DELETE_DEFAULT_ERROR, SysMessages.DELETE_FAIL);
        }

        return ResultVO.success(delRows, SysMessages.DELETE_SUCCESS);
    }

    /**
     * 通过分类id查询菜品列表
     * @param dishDTO
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<DishVO>> queryDishesByCategoryId(DishDTO dishDTO) {
        List<DishVO> dishVOS = dishService.listDishes(dishDTO);

        return ResultVO.success(dishVOS, SysMessages.QUERY_SUCCESS);
    }
}
























